package cn.tannn.cat.file.server.controller.manage;

import cn.tannn.cat.file.server.controller.manage.dto.DictAppend;
import cn.tannn.cat.file.server.controller.manage.dto.DictPage;
import cn.tannn.cat.file.server.controller.manage.dto.DictSortEdit;
import cn.tannn.cat.file.server.controller.manage.vo.DictVO;
import cn.tannn.cat.file.server.entity.Dict;
import cn.tannn.cat.file.server.service.DictService;
import cn.tannn.jdevelops.annotations.web.mapping.PathRestController;
import cn.tannn.jdevelops.jpa.constant.SQLOperator;
import cn.tannn.jdevelops.jpa.result.JpaPageResult;
import cn.tannn.jdevelops.result.response.ResultPageVO;
import cn.tannn.jdevelops.result.response.ResultVO;
import cn.tannn.jdevelops.result.utils.ListTo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * 字典接口
 *
 * @author <a href="https://tannn.cn/">tan</a>
 * @date 2024/3/8 9:30
 */
@Tag(name = "字典管理", description = "字典接口")
@PathRestController("dict")
@RequiredArgsConstructor
@Slf4j
public class DictController {

    private final DictService dictService;

    @Operation(summary = "新增字典")
    @PostMapping("append")
    public ResultVO<String> append(@RequestBody @Valid DictAppend append) {
        dictService.append(append.toDict());
        return ResultVO.successMessage("字典新增成功");
    }

    @Operation(summary = "查询字典-分页")
    @PostMapping("page")
    public ResultPageVO<DictVO, JpaPageResult<DictVO>> page(@RequestBody @Valid DictPage page) {
        Page<Dict> dicts = dictService.findPage(page, page.getPage());
        JpaPageResult<DictVO> result = JpaPageResult.toPage(dicts, DictVO.class);
        return ResultPageVO.success(result, "查询成功");
    }

    @Operation(summary = "查询字典")
    @GetMapping("list")
    public ResultVO<List<DictVO>> listDict() {
        List<Dict> dicts = dictService.fileDict();
        List<DictVO> vos = ListTo.to(DictVO.class, dicts);
        return ResultVO.success(vos);
    }

    @Operation(summary = "批量删除字典")
    @PostMapping("deletes")
    public ResultVO<String> deletes(@RequestBody List<Long> ids) {
        dictService.delete("id", SQLOperator.IN, ids);
        return ResultVO.successMessage("所选字典被删除成功");
    }

    @Operation(summary = "批量启用字典")
    @PostMapping("enables")
    public ResultVO<String> enables(@RequestBody List<Long> ids) {
        dictService.enables(ids);
        return ResultVO.successMessage("所选字典启用成功");
    }

    @Operation(summary = "批量停用字典")
    @PostMapping("disable")
    public ResultVO<String> disable(@RequestBody List<Long> ids) {
        dictService.disable(ids);
        return ResultVO.successMessage("所选字典停用成功");
    }

    @Operation(summary = "字典排序")
    @PostMapping("sort")
    public ResultVO<String> sort(@RequestBody @Valid DictSortEdit sort) {
        dictService.sort(sort);
        return ResultVO.successMessage("字典排序成功");
    }

}
